﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication4 {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {

        public DataSet mySet { get; set; }
        public readonly int PageSize = 5;
        public int PageIndex = 0;
        public int Count = 0;

        public MainWindow() {
            InitializeComponent();

            PrepareData();

            CreateOrUpdateTables();

            UpdateItemsSource();

            updatePageTxtBox();
        }

        private void UpdateItemsSource() {
            originalGrid.ItemsSource = mySet.Tables["PagedTable"].DefaultView;
            newGrid.ItemsSource = mySet.Tables["NewTable"].DefaultView;
        }

        private void CreateOrUpdateTables() {
            if (mySet.Tables.IndexOf("PagedTable") != -1) {
                mySet.Tables.Remove("PagedTable");
            }
            mySet.Tables.Add(mySet.Tables["OriginalData"].Paging("PagedTable", PageSize, PageIndex));

            if (mySet.Tables.IndexOf("NewTable") != -1) {
                mySet.Tables.Remove("NewTable");
            }
            mySet.Tables.Add(mySet.Tables["PagedTable"].Pivot("NewTable", "Date"));
        }

        private DataSet PrepareData() {
            mySet = new DataSet();
            DataTable dt = new DataTable("OriginalData");
            mySet.Tables.Add(dt);

            //dt.Columns.Add(new DataColumn("Type", typeof(string)));
            dt.Columns.Add(new DataColumn("Date", typeof(DateTime)));
            dt.Columns.Add(new DataColumn("HeightValue", typeof(decimal)));
            dt.Columns.Add(new DataColumn("WeightValue", typeof(decimal)));

            DateTime date = DateTime.Today;
            for (int i = 0; i < 30; i++) {
                dt.Rows.Add(dt.NewRow());
                dt.Rows[i][0] = date.AddDays(i);
                dt.Rows[i][1] = 100 + i;
                dt.Rows[i][2] = 40 + i;
            }
            
            this.Count = dt.Rows.Count;
            return mySet;
        }

        private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            DataGrid dg = sender as DataGrid;
            DataRowView dr = dg.SelectedItem as DataRowView;
            LineSeries series = myChart.Series[0] as LineSeries;
            if (dr != null) {
                string selectedType = dr["Type"].ToString();
                
                //var data = mySet.Tables["PagedTable"].Select().Where(x => x["Type"].ToString() == selectedType).OrderBy(x=>x["Date"]).ToList();
                var data = mySet.Tables["PagedTable"].Select().OrderBy(x => x["Date"]).ToList();
                List<KeyValuePair<DateTime, decimal>> chartData = new List<KeyValuePair<DateTime, decimal>>();
                foreach (var d in data) {
                    chartData.Add(new KeyValuePair<DateTime, decimal>(DateTime.Parse(d["Date"].ToString()), Decimal.Parse(d[selectedType].ToString())));
                }
                series.ItemsSource = chartData;
            } else {
                series.ItemsSource = null;
            }
        }

        private void updatePageTxtBox() {
            PageTxtBox.Text = (PageIndex + 1).ToString();
        }

        private void PrevBtn_Click(object sender, RoutedEventArgs e) {
            if (PageIndex == 0)
                return;
            this.PageIndex--;
            CreateOrUpdateTables();
            updatePageTxtBox();
            UpdateItemsSource();
        }

        private void NextBtn_Click(object sender, RoutedEventArgs e) {
            if (PageIndex * PageSize > Count)
                return;
            this.PageIndex++;
            CreateOrUpdateTables();
            updatePageTxtBox();
            UpdateItemsSource();
        }

        
    }
}
